﻿using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using _3D.Data;

namespace _3D
{
    public partial class FormMain : Form
    {
        private AutoResetEvent _EventLogo;

        private Game CurrentGame;
        private myPictureBox picMaze;

        private static string name;
        private int msx;
        private int msy;
        private int smooth;
        private int x, y;

        public FormMain()
        {
            InitializeComponent();
            InitializeMyComponent();
            CurrentGame = new Game();
        }


        private void btnCreate_Click(object sender, System.EventArgs e)
		{
			try
			{
				if ( !Varia.IsNumeric( txtMSX.Text ) ||	!Varia.IsNumeric( txtMSY.Text ) )
				{
					MessageBox.Show ( "Parameters should be numeric.", "Bad parameters", MessageBoxButtons.OK, MessageBoxIcon.Warning ); 
					return;
				}

				msx  = int.Parse ( txtMSX.Text );
				msy  = int.Parse ( txtMSY.Text );
                smooth = int.Parse ( txtSmooth.Text );

                CurrentGame.Start(msx, msy, smooth);
                picMaze.cX = 0;
                picMaze.cY = 0;
                
                picMaze.Image = CurrentGame.GetBitmap();
                this.Refresh();
            }
			catch ( Exception ex )
			{
				MessageBox.Show ( "Exception caught: " + ex.Message + "\nBetter luck next time." );
			}
		}

		private void btnSolve_Click(object sender, System.EventArgs e)
		{
            try
            {
                ArrayList solvePath = CurrentGame.Solve();

                int xSize = picMaze.Image.Width / msx;
                int ySize = picMaze.Image.Height / msy;

                int minxSyS = (xSize > ySize ? ySize : xSize);
                int penSize = (minxSyS > 16 ? minxSyS / 16 : 1);

                Pen p = new Pen(Color.Red, penSize);
                Graphics g = Graphics.FromImage((Image)picMaze.Image);

                if (solvePath != null)
                    for (int i = 1; i < solvePath.Count; i++)
                    {
                        Game.cCellPosition u = (Game.cCellPosition)solvePath[i - 1];
                        Game.cCellPosition t = (Game.cCellPosition)solvePath[i];
                        g.DrawLine(p,
                                   xSize * u.x + xSize / 2,
                                   ySize * u.y + ySize / 2,
                                   xSize * t.x + xSize / 2,
                                   ySize * t.y + ySize / 2);
                    }
                p.Dispose();
                g.Dispose();

                this.picMaze.Image = CurrentGame.GetBitmap();
                this.Refresh();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Exception caught: " + ex.Message + "\nBetter luck next time.");
            }
		}

		private void btnAbout_Click(object sender, System.EventArgs e)
		{
			(new FormAbout()).ShowDialog();
		}

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                (new FormName()).ShowDialog();
                if (name != "")
                    { CurrentGame.Save(name, picMaze.cX, picMaze.cY); }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception caught: " + ex.Message + "\nBetter luck next time.");
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                (new FormName()).ShowDialog();
                if (name != "")
                {
                    CurrentGame.Load(ref msx, ref msy, ref smooth, name, ref x, ref y);
                    picMaze.cX = x;
                    picMaze.cY = y;
                    txtMSX.Text = msx.ToString();
                    txtMSY.Text = msy.ToString();
                    txtSmooth.Text = smooth.ToString();

                    picMaze.Image = CurrentGame.GetBitmap();
                    this.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception caught: " + ex.Message + "\nBetter luck next time.");
            }
        }

        private void btnGame_Click(object sender, EventArgs e)
        {
            try
            {
                msx = int.Parse(txtMSX.Text);
                msy = int.Parse(txtMSY.Text);
                smooth = int.Parse(txtSmooth.Text);

                picMaze.Select();
                picMaze.SetGame(ref CurrentGame, msx, msy);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception caught: " + ex.Message + "\nBetter luck next time.");
            }
        }

        private void btn3D_Click(object sender, EventArgs e)
        {
            Form3D frm3d = new Form3D();
            frm3d.Show();
        }

        private void btnSaveXML_Click(object sender, EventArgs e)
        {
            try
            {
                (new FormName()).ShowDialog();
                if (name != "")
                    { CurrentGame.Serialize(CurrentGame, picMaze.cX, picMaze.cY, name); }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception caught: " + ex.Message + "\nBetter luck next time.");
            }
        }

        private void btnLoadXML_Click(object sender, EventArgs e)
        {
            try
            {
                (new FormName()).ShowDialog();
                if (name != "")
                {
                    CurrentGame.Deserialize(ref msx, ref msy, ref smooth, name, ref x, ref y);
                    picMaze.cX = x;
                    picMaze.cY = y;
                    txtMSX.Text = msx.ToString();
                    txtMSY.Text = msy.ToString();
                    txtSmooth.Text = smooth.ToString();

                    picMaze.Image = CurrentGame.GetBitmap();
                    this.Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception caught: " + ex.Message + "\nBetter luck next time.");
            }
        }

        public static string PlayerName
        {
            get
                {return name;}
            set
                { name = value; }
        }

    }
}
